package com.hnqcgczy.db;

import java.sql.Connection;
import java.sql.SQLException;

public class MyTransaction {
   private Connection connection;

    public  MyTransaction(boolean isAutoCommit) {
        try {

           // MyConnection myConnection = new MyConnection();//组件
           // this.connection=myConnection.getConnectionByMysql();
            this.connection = new MyConncetion().getConnectionByMysql();
          // this.connection = new MyConn().getConnectionByMysql();


            this.connection.setAutoCommit(isAutoCommit);//设置数据库连接的一个属性值
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 获取设置了事务的数据库连接
     * @return
     */
    public Connection getConn(){

        return connection;
    }

    /**
     * 关闭数据库
     */
    public void closeConn(){
        try {
            if(connection!=null&&!connection.isClosed()){
                connection.close();

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public void commit(){
        try {
            connection.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public void rollback(){
        try {
            connection.rollback();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

//    public void setConnection(boolean is) {
//        try {
//            this.connection.setAutoCommit(is);
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }
//    }
}
